import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { CommonService } from '../../../../services-common/common.service';
import { secondOrderStateArrs } from '../../status.pipe';
import { FinanceSecondManageService } from '../finance-second-manage.service';

@Component({
  selector: 'app-finance-second-order-report',
  templateUrl: './finance-second-order-report.component.html',
  styleUrls: ['./finance-second-order-report.component.scss']
})
export class FinanceSecondOrderReportComponent implements OnInit {

  searchOptions: any = [
    {
      type: 2,
      labelText: '订单状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...secondOrderStateArrs]
    },
    {
      type: 2,
      labelText: '货主',
      valueKey: 'supplierId',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'name' },
      selectOptions: []
    },
    { type: 1, labelText: '交易流水', valueKey: 'payOrderNum', value: '' },
    { type: 3, labelText: '下单时间', valueKey: ['createBeginTime', 'createEndTime'], value: [], isTimeSelect: true },
    { type: 3, labelText: '付款时间', valueKey: ['payBeginTime', 'payEndTime'], value: [], isTimeSelect: true },
    { type: 3, labelText: '发货时间', valueKey: ['shipBeginTime', 'shipEndTime'], value: [], isTimeSelect: true },
    { type: 3, labelText: '完成时间', valueKey: ['dealBeginTime', 'dealEndTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };
  exportLoading = false;

  supplierOptions: any[] = [];

  constructor(
    private msg: NzMessageService,
    private modal: NzModalService,
    private commonService: CommonService,
    private services: FinanceSecondManageService
  ) { }

  ngOnInit(): void {
    this.loadSupplierOptions();
  }

  // 查询货主
  async loadSupplierOptions() {
    let params = {
      page: 1,
      pageSize: 9999
    }
    const res: any = await this.commonService.querySupplierList(params);
    if (res.code === 0) {
      const { records } = res.data || {};

      this.supplierOptions = records.filter((item: any) => item.name);
      this.setSearchOption(this.supplierOptions, 'supplierId');
    } else {
      this.msg.error(res.message);
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getSecondOrderReportList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.msg.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
  
  exportTableData() {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的数据，为了导出速度请尽可能的控制时间范围。确认导出？',
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        this.exportLoading = true;
        const res: any = await this.services.exportSecondOrderReportData({...this.searchParams});
        this.exportLoading = false;
        
        if (res.type === 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '二手-订单报表');
        } else {
          this.msg.error('导出失败');
        }
      },
      nzCancelText: '取消'
    });
  }

  setSearchOption(array: Array<any>, val: string) {
    let temIndex = this.searchOptions.findIndex((item: any) => item.valueKey === val);
    this.searchOptions[temIndex].selectOptions = array || [];
  }
}
